package router

import (
	"net/http"

	"apiserver/handler/sd"
	"apiserver/router/middleware"

	"github.com/gin-gonic/gin"
	"apiserver/handler/user"
	"apiserver/handler/post"
)

// 加载路由,中间件,和控制器
func Load(g *gin.Engine, mw ...gin.HandlerFunc) *gin.Engine {
	// 中间件
	g.Use(gin.Recovery())
	g.Use(middleware.NoCache)
	g.Use(middleware.Options)
	g.Use(middleware.Secure)
	g.Use(mw...)
	// 404 Not Find
	g.NoRoute(func(c *gin.Context) {
		c.String(http.StatusNotFound, "无效的请求地址")
	})
	g.POST("/login", user.Login) //用户登录
	g.POST("/sign", user.Create) //用户注册

	//用户模块路由
	u := g.Group("/v1/user")
	u.Use(middleware.AuthMiddleware())
	{
		u.POST("", user.Create)        //创建用户
		u.DELETE("/:uid", user.Delete) //删除用户
		u.PUT("/:uid", user.Update)    //更新用户
		u.GET("", user.List)           //用户列表
		u.GET("/:uid", user.Get)       //获取用户详情
	}
	//帖子模块路由
	p := g.Group("/v1/post")
	p.GET("", post.List) //微贴列表
	p.Use(middleware.AuthMiddleware())
	{
		p.POST("/add", post.Create)    //创建微贴
		p.GET("/:pid", post.Detail)    //微贴详情
		p.DELETE("/:pid", post.Delete) //删除微贴
	}

	//获取磁盘性能用来验证api是否正确
	svcd := g.Group("/sd")
	{
		svcd.GET("/health", sd.HealthCheck)
		svcd.GET("/disk", sd.DiskCheck)
		svcd.GET("/cpu", sd.CPUCheck)
		svcd.GET("/ram", sd.RAMCheck)
	}

	return g
}
